import ZJRequest from './request'
import { BASE_URL, TIME_OUT } from './request/config'
import localCache from '@/utils/cache'
import type { KZJRequestConfig } from './request/type'

const zjRequest = new ZJRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  showLoading: false, //默认接口没有Loading动画效果
  takeToken: false,
  takeCookie: false,
  interceptors: {
    //请求成功的拦截
    requestInterceptor: (config: KZJRequestConfig) => {
      //携带token拦截
      // console.log('默认配置的请求成功拦截')
      const token = localCache.getCache('token') ?? ''
      if (token && config.headers && config.takeToken) {
        console.log('加上了Token')
        config.headers.Authorization = `Bearer ${token}`
      }
      const cookie = localCache.getCache('cookie') ?? ''
      if (cookie && config.takeCookie) {
        console.log('加上了Cookie')
        config.data.cookie = cookie
      }

      return config
    },
    //请求失败的拦截
    requestInterceptorCatch: (err) => {
      console.log('请求失败的拦截' + err)
      return err
    },
    //响应成功的拦截
    responseInterceptor: (res) => {
      // console.log('默认配置的响应成功拦截')
      return res
    },
    //响应失败的拦截
    responseInterceptorCatch: (err) => {
      console.log('响应失败的拦截' + err)
      console.log(err)
      console.log(err.msg)
      return err
    }
  }
})
export default zjRequest
